package com.yang.jxj.Utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yang.jxj.entity.Rules;
import com.yang.jxj.entity.Score;
import com.yang.jxj.entity.Students;
import com.yang.jxj.service.impl.RulesServiceImpl;
import com.yang.jxj.service.impl.ScoreServiceImpl;
import com.yang.jxj.service.impl.StudentsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class totalScore {

    @Autowired
    private StudentsServiceImpl stu;
    @Autowired
    private ScoreServiceImpl score;
    @Autowired
    private RulesServiceImpl rule;
    @Autowired
    private ObjectMapper jacksonObjectMapper;


    public void TotalScore(Integer userId) {
        Students byId = stu.getById(userId);
        Integer enrollmentYear = byId.getEnrollmentYear();
        String grade = byId.getGrade();
        QueryWrapper<Rules> Wrapper = new QueryWrapper<>();
        Wrapper.eq("year", enrollmentYear);
        Wrapper.eq("grade", grade);
        Rules ruleOne = rule.getOne(Wrapper);
        Score scoreById = score.getById(userId);
        JsonNode jsonNode;
        int creatT; //科研最高分
        double grade1; //成绩权重
        int fuwu; //服务附加最高分
        int gradeT; //成绩最高分
        int thinkT; // 思想最高分
        int fufen; // 附加最高分
        try {
           jsonNode = jacksonObjectMapper.readTree(ruleOne.getCriteria());
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        JsonNode creatTNode = jsonNode.get("creatT");
        JsonNode gradeNode = jsonNode.get("grade");
        JsonNode fuwuNode = jsonNode.get("fuwu");
        JsonNode fufenNode = jsonNode.get("fufen");
        JsonNode gradeTNode = jsonNode.get("gradeT");
        JsonNode thinkNode = jsonNode.get("thinkT");
        if (creatTNode != null) {
             creatT = creatTNode.asInt(); // 使用0作为默认值
            // 使用creatT进行后续操作
        }else{
            creatT=0;
        }
        if(gradeNode != null){
            grade1 = gradeNode.asDouble();
        }else{
            grade1=0;
        }
        if(fuwuNode != null){
            fuwu = fuwuNode.asInt();
        }else{
            fuwu=0;
        }
        if(fufenNode != null){
            fufen = fufenNode.asInt();
        }else{
            fufen=0;
        }
        if(gradeTNode != null){
            gradeT = gradeTNode.asInt();
        }else{
            gradeT=0;
        }
        if(thinkNode != null){
            thinkT = thinkNode.asInt();
        }else{
            thinkT=0;
        }

        if(ruleOne.getGrade().equals("研一")){
            System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+grade1);
            scoreById.setTotal((scoreById.getAcScore()/(double)scoreById.getAcCount()*grade1));
            score.saveOrUpdate(scoreById);
        }else{
            double score1 = (scoreById.getAcScore()/(double)scoreById.getAcCount()*grade1);
            if(score1>gradeT){
                score1 = gradeT;

            }
            if(scoreById.getCreatScore() > creatT){
                scoreById.setCreatScore(creatT);
            }
            if(scoreById.getFujScore() > fufen){
                scoreById.setFujScore(fufen);
            }
            if(scoreById.getIdeologyScore() > thinkT){
                scoreById.setIdeologyScore(thinkT);
            }
            if(scoreById.getSerScore() > fuwu){
                scoreById.setSerScore(fuwu);
            }
            scoreById.setTotal(score1+scoreById.getCreatScore()+scoreById.getFujScore()+scoreById.getIdeologyScore()+scoreById.getSerScore());
            score.saveOrUpdate(scoreById);
        }



    }
}
